package com.fnst.caesar.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;

import com.fnst.caesar.bean.*;
import com.fnst.caesar.util.SqlFactoryUtil;

/**
 * Servlet implementation class Login_UI
 */
public class Login_Servlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login_Servlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		String user_id = request.getParameter("user_id");
		String user_password = request.getParameter("user_password");
		
		if(user_id == null || user_password == null)
		{
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}
		else
		{
			UserBasic_Bean ubb = new UserBasic_Bean();
			ubb.setUser_id(user_id);
			ubb.setUser_password(user_password);
			
			SqlSessionFactory ssf = SqlFactoryUtil.getSqlSessionFactory(request);
			
			SqlSession sqlSession = ssf.openSession();
			UserBasic_Bean result = (UserBasic_Bean)sqlSession.selectOne("com.fnst.caesar.mapper.selectUserBasicByUserId",ubb.getUser_id());
			if(result == null)
			{
				request.setAttribute("error", "login_err");
				request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
			}
			else
			{
				if(ubb.getUser_password().equals(result.getUser_password()))
				{
					response.sendRedirect("/main");
				}
				else
				{
					request.setAttribute("error", "login_err");
					request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
				}
			}
		}
	}

}
